#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>
#include <QTextEdit>
#include "literaryframework.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    //初始化ui
    ui->setupUi(this);

//    QListWidget* pListWnd = new QListWidget();
//    ui->mdiArea->addSubWindow(pListWnd);
//    QWidget*pWidget =  new LiteraryFramework();
//    pWidget->showMaximized();
//    ui->mdiArea->addSubWindow(pWidget);
//    pWidget->resize(ui->mdiArea->size());



    //初始化信号与槽
    SetConnects();

    //初始化其他指针
    //初始化文件管理系统指针
    m_chapter_auto_save_manager = new ChapterAutoSaveManager(m_binChapterFileAddress.toStdString(),3000);


    //需要根据其他功能更新的ui
    InitChapterList();//初始化章节列表
}

MainWindow::~MainWindow()
{
    delete ui;
    m_chapter_auto_save_manager->SaveFile();
}

void MainWindow::SetConnects()
{
    connect(ui->pushButton_addNewChapter,&QPushButton::clicked,this,&MainWindow::OnClickedAddNewChapter);
    connect(ui->listWidget_MainChapters, &QListWidget::currentItemChanged,this,&MainWindow::OnMainChapterListChanged);
    connect(ui->textEdit,&QTextEdit::textChanged,this,&MainWindow::OnTextEditChanged);
    connect(ui->pushButton_DeleteChapter,&QPushButton::clicked,this,&MainWindow::OnClickedDeleteChapter);
}

void MainWindow::SetChapterList()
{
    if(m_chapter_auto_save_manager==nullptr)
        return;
    ui->listWidget_MainChapters->clear();

}

QListWidgetItem* MainWindow::CreateNewChapter(const QString &title, const QString &content, const QString &summary)
{
    QListWidgetItem* pitem = new QListWidgetItem();
    pitem->setData(Qt::DisplayRole,title);
    pitem->setData(Qt::UserRole,content);
    pitem->setData(Qt::UserRole+1,summary);
    return pitem;
}

void MainWindow::ResetChapterNumber()
{
    size_t chaptersize = m_chapter_auto_save_manager->CountChapters();
}

void MainWindow::OnClickedAddNewChapter()
{
    QString title = "", content = "", summary = "";

    QString tmp;
    size_t idx = m_chapter_auto_save_manager->CountChapters();
//    tmp = "new chapter";
//    title = QString("第%1章:").arg(idx+1) + tmp;
    title = QString("第%1章").arg(idx+1);
    m_chapter_auto_save_manager->add_chapter(title.toStdString(),content.toStdString(),summary.toStdString());
    QListWidgetItem* pitem = CreateNewChapter(title, content, summary);
    ui->listWidget_MainChapters->addItem(pitem);

    m_chapter_auto_save_manager->SaveFile();
}

void MainWindow::OnClickedDeleteChapter()
{
    QListWidgetItem*pitem = ui->listWidget_MainChapters->currentItem();
    int idx = ui->listWidget_MainChapters->currentRow();
    m_chapter_auto_save_manager->remove_chapter(idx);
    ui->listWidget_MainChapters->removeItemWidget(pitem);
    delete pitem;
}

void MainWindow::OnMainChapterListChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    QString content = current->data(Qt::UserRole).toString();
    OnShowTextBrowser(content);
}

void MainWindow::OnTextEditChanged()
{
    QString content = ui->textEdit->toPlainText();
    QListWidgetItem*pitem = ui->listWidget_MainChapters->currentItem();
    pitem->setData(Qt::UserRole,content);
    m_chapter_auto_save_manager->update_chapter(ui->listWidget_MainChapters->currentRow(),
                                                content.toStdString());
}

void MainWindow::OnShowTextBrowser(const QString &content)
{
    ui->textEdit->setText(content);
}

void MainWindow::InitChapterList()
{
    if(m_chapter_auto_save_manager==nullptr)
        return;
     QString qtitle = "", qcontent = "", qsummary = "";
    for(size_t i=0;i<m_chapter_auto_save_manager->CountChapters();++i)
    {
        std::string tittle,content,summary;
        m_chapter_auto_save_manager->get_chapter(i,tittle,content,summary);
        qtitle = QString::fromStdString(tittle);
        qcontent = QString::fromStdString(content);
        qsummary = QString::fromStdString(summary);
        QListWidgetItem* pitem = CreateNewChapter(qtitle, qcontent, qsummary);
        ui->listWidget_MainChapters->addItem(pitem);
    }
    ui->listWidget_MainChapters->setCurrentRow(0);
}
